package com.xin.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xin.mmall.entity.*;
import com.xin.mmall.service.CartService;
import com.xin.mmall.service.ProductCategoryService;
import com.xin.mmall.service.ProductService;
import com.xin.mmall.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public ModelAndView add(@PathVariable("productId") Integer productId,
                      @PathVariable("price") float price,
                      @PathVariable("quantity") Integer quantity,
                      HttpSession session){
        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setCost(price*quantity);
        User user = (User) session.getAttribute("user");
        cart.setUserId(user.getId());
        ModelAndView modelAndView = new ModelAndView();
        try {
            if (cartService.save(cart)){
                modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
                modelAndView.setViewName("settlement1");
                modelAndView.addObject("list",cartService.findAllCartVOByUserId(user.getId()));
            }
        } catch (Exception e) {
            System.out.println("库存不足");
            modelAndView.setViewName("productDetail");
            modelAndView.addObject("error","库存不足！");
            modelAndView.addObject("product",productService.getById(productId));
            modelAndView.addObject("list",productCategoryService.getAllProductCategoryVO());
        }
        return modelAndView;
    }
    //查询所有购物车记录
    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user = (User) session.getAttribute("user");
        if (user==null){
            modelAndView.addObject("cartList",new ArrayList());
        }else{
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }


    //删除购物车记录
    @GetMapping("/deleteById/{id}/{quantity}")
    public String deleteById(@PathVariable("id") Integer id,@PathVariable("quantity") Integer quantity){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("quantity",quantity);
        cartService.removeByMap(map);
        return "redirect:/cart/findAllCart";
    }

    //购物车跳转
    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User) session.getAttribute("user");
        //返回用户地址列表
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getId());
        modelAndView.addObject("addressList",userAddressService.list(wrapper));
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //动态更新购物车记录
    @ResponseBody
    @PostMapping("/update/{id}/{quantity}/{cost}")
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost){
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);
        if (cartService.updateById(cart))
            return "success";
        return "fail";
    }

}

